﻿using System;
using System.Linq;
using ClosedXML.Excel;
using SuppKids.Core.Domain;
using SuppKids.Core.Extensions;
using SuppKids.Core.Reporting.Data;
using SuppKids.Core.Stuff;
using SuppKids.Resources;

namespace SuppKids.Core.Reporting.Excel
{
	public class ParentsReportGenerator : ExcelReportGenerator
	{
		private readonly StuffInfoStorage stuffInfoStorage;

		public ParentsReportGenerator(StuffInfoStorage stuffInfoStorage)
		{
			this.stuffInfoStorage = stuffInfoStorage;
		}

		protected override void CreateContent(IXLWorksheet worksheet, IReportData reportData)
		{
			var stuffInfo = stuffInfoStorage.Get();
			var data = reportData as ParentsReportData;
			if (data == null)
			{
				throw new InvalidOperationException("Report data is not valid for this kind of report.");
			}

            // костыль для сменя позиций Ясли\Сад
            //data.GroupsData.Insert(0, data.GroupsData[1]);
            //data.GroupsData.RemoveAt(2);

			worksheet.Column("A").Width = 50;
			worksheet.Column("B").Width = 25;

			worksheet.Style.Font.FontName = "Arial";
			worksheet.Style.Font.FontSize = 10;
			worksheet.Style.Font.Bold = false;

			var title = worksheet.Range("A1:B1").Merge();
			title.Value = Labels.Label_ParentsMenu;
			title.Style.Font.SetUnderline(XLFontUnderlineValues.Single);
			title.Style.Font.FontSize = 16;
			title.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
			title.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;

			worksheet.Cell("A2").Value = Labels.TableHeader_DailyMenu;
			worksheet.Cell("A3").Value = Labels.Label_DayOfWeek;
			worksheet.Cell("A4").Value = Labels.Label_WeekNumber;
			worksheet.Cell("A5").Value = Labels.Label_MenuType;
			worksheet.Cell("A6").Value = Labels.Label_DietSestra;
			worksheet.Cell("A7").Value = Labels.Label_Vospitatel;
			worksheet.Cell("A8").Value = string.Format("{0} - {1}", Labels.Label_ChildrenCount, data.GroupsData.Aggregate(string.Empty, (s, groupData) => s + groupData.Group.Name + "\\").TrimEnd('\\'));

			worksheet.Cell("B2").Value = DateTime.Now.ToString(Common.DateFormat);
			worksheet.Cell("B3").Value = data.DayOfWeek.GetLocalizedEnumValue();
			worksheet.Cell("B4").Value = data.WeekNumber.GetLocalizedEnumValue();
			worksheet.Cell("B5").Value = data.MenuType.GetLocalizedEnumValue();
			worksheet.Cell("B6").Value = stuffInfo.DietSestra;
			worksheet.Cell("B7").Value = stuffInfo.Vospitatel;

            uint[] childreCountGroups = new uint[data.GroupsData.Count()];
            for (int i = 0; i < childreCountGroups.Count(); i++)
                childreCountGroups[i] = 0;
            foreach (var dailyMenu in data.DailyMenus)
            {
                int g = 0; 
                foreach (var groupData in data.GroupsData)
                {
                    childreCountGroups[g] += groupData.ChildrenCount[dailyMenu.MealType];
                    g++;
                }
            }
            int j = 0;
            foreach (GroupData group in data.GroupsData)
            {
                worksheet.Cell("B8").Value = worksheet.Cell("B8").Value + childreCountGroups[j].ToString() + "/";
                j++;
            }
            worksheet.Cell("B8").Value = worksheet.Cell("B8").Value.ToString().TrimEnd('/');
      
			//worksheet.Cell("B8").Value = data.GroupsData.Aggregate(string.Empty, (s, groupData) => s + /*groupData.ChildrenCount + */"\\").TrimEnd('\\');
			
			worksheet.Range("B2:B5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
			
			worksheet.Cell("A10").Value = Labels.TableHeader_MealType;
			worksheet.Cell("B10").Value = string.Format("{0}: {1}", Labels.Label_ChildrenCount, data.GroupsData.Aggregate(string.Empty, (s, groupData) => s + groupData.Group.Name + " г.\\").TrimEnd('\\'));
			var header = worksheet.Range("A10:B10");
			header.Style.Fill.BackgroundColor = XLColor.LightGray;
			header.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
			header.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
			header.Style.Alignment.SetWrapText(true);

			var index = 11;
			foreach (var dailyMenu in data.DailyMenus.Where(a => a.Dishes.Any()).OrderBy(a => a.MealType))
			{
				var cell = worksheet.Cell(string.Format("A{0}", index));
				cell.Style.Fill.BackgroundColor = XLColor.LightGray;
				cell.Style.Font.Bold = true;
				cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;

				cell.Value = dailyMenu.MealType.GetLocalizedEnumValue();
				worksheet.Range(string.Format("A{0}:B{0}", index)).Merge();
				index++;

				foreach (var dish in dailyMenu.Dishes)
				{
					var cellA = worksheet.Cell(string.Format("A{0}", index));
					var cellB = worksheet.Cell(string.Format("B{0}", index));

					cellA.Value = dish.Name;
					cellB.Value = data.GroupsData.Select(groupData => dish.DishAmounts.FirstOrDefault(a => a.Group.Id == groupData.Group.Id && a.DailyMenu.Id == dailyMenu.Id) ?? new DishAmount(groupData.Group, dailyMenu)).Aggregate("", (current, amount) => current + (amount.Amount + "г.\\")).TrimEnd('\\');
					index++;
				}
			}

			var range = worksheet.Range(string.Format("A10:B{0}", index - 1));
			range.Style.Border.OutsideBorder = XLBorderStyleValues.Medium;
			range.Style.Border.InsideBorder = XLBorderStyleValues.Thin;
		}
	}
}